using System.Collections.Generic;
using UnityEngine;

public class BCWDebug : MonoBehaviour
{
	public static BCWDebug instance;

	public int messageCount = 250;

	private Dictionary<string, List<BCWDebugMessageLine>> lineDictionary;

	private bool showLog;

	private bool pause;

	private string showGroup;

	private Vector2 scrollPosition = Vector2.zero;

	private Vector2 scrollGroupPosition = Vector2.zero;

	private float minBtnWidth;

	private float minBtnHeight;

	public Color colorAsert = Color.magenta;

	public Color colorError = Color.red;

	public Color colorException = Color.cyan;

	public Color colorLog = Color.gray;

	public Color colorWarning = Color.yellow;

	public bool enableWorning = true;

	public bool enableAssert = true;

	public bool enableException = true;

	public bool enableError = true;

	public bool enableLog = true;

	public bool maximize;

	public float cHeight;

	public Rect consoleMaxRect;

	private GUIStyle lineStyle;

	private float deltaTime;

	private float averageDeltaTime;

	private float tmpTime;

	public static bool isDebugBuild
	{
		get
		{
			return false;
		}
	}

	private void Awake()
	{
		if (instance != null)
		{
			Object.Destroy(this);
		}
		else
		{
			Application.logMessageReceived += HandleLog;
			instance = this;
		}
		Object.DontDestroyOnLoad(this);
		if (instance.lineDictionary == null)
		{
			instance.lineDictionary = new Dictionary<string, List<BCWDebugMessageLine>>();
		}
		instance.lineStyle = new GUIStyle();
		enableWorning = true;
		enableAssert = true;
		enableException = true;
		enableError = true;
		enableLog = true;
		maximize = false;
	}

	private static void Init()
	{
		if (instance == null)
		{
			GameObject gameObject = Object.Instantiate(new GameObject());
			gameObject.AddComponent<BCWDebug>();
			instance = gameObject.GetComponent<BCWDebug>();
			gameObject.name = "BCWDebug";
			Application.logMessageReceived += HandleLog;
		}
	}

	private Color GetColorFromType(LogType type)
	{
		switch (type)
		{
		case LogType.Assert:
			return colorAsert;
		case LogType.Error:
			return colorError;
		case LogType.Exception:
			return colorException;
		case LogType.Log:
			return colorLog;
		case LogType.Warning:
			return colorWarning;
		default:
			return colorLog;
		}
	}

	private static void HandleLog(string logString, string stackTrace, LogType type)
	{
		Log(logString + "\n" + stackTrace, "Unity Log", type);
	}

	public static void Log(object message, string group = "General", LogType type = LogType.Log, int position = -1)
	{
		Log(message, group, type, position, Vector4.zero);
	}

	public static void LogError(object message, string group = "General", LogType type = LogType.Error, int position = -1)
	{
		Log(message, group, type, position, Vector4.zero);
	}

	public static void LogWarning(object message, string group = "General", LogType type = LogType.Warning, int position = -1)
	{
		Log(message, group, type, position, Vector4.zero);
	}

	public static void Log(object message, string group, LogType type, int position, Color color)
	{
	}

	private bool ChkEquals(object message, Color color, string group)
	{
		for (int i = 0; i < lineDictionary[group].Count; i++)
		{
			if (message.ToString() == lineDictionary[group][i].message && color == lineDictionary[group][i].color)
			{
				BCWDebugMessageLine value = lineDictionary[group][i];
				value.count++;
				lineDictionary[group][i] = value;
				return true;
			}
		}
		return false;
	}

	private bool CompareWisibleStatus(BCWDebugMessageLine line)
	{
		if (enableWorning && line.type == LogType.Warning)
		{
			return true;
		}
		if (enableAssert && line.type == LogType.Assert)
		{
			return true;
		}
		if (enableException && line.type == LogType.Exception)
		{
			return true;
		}
		if (enableError && line.type == LogType.Error)
		{
			return true;
		}
		if (enableLog && line.type == LogType.Log)
		{
			return true;
		}
		return false;
	}

	public void setMaxWidth(BCWDebugMessageLine line, KeyValuePair<string, List<BCWDebugMessageLine>> pair)
	{
		BCWDebugMessageLine value = new BCWDebugMessageLine(line);
		value.color = line.color;
		if (value.maxHeight == 0)
		{
			if (value.maxHeight == 250)
			{
				value.maxHeight = 30;
			}
			else
			{
				value.maxHeight = 250;
			}
		}
		int index = instance.lineDictionary[pair.Key].IndexOf(line);
		instance.lineDictionary[pair.Key][index] = value;
	}

	public void ClearAllLog()
	{
		instance.lineDictionary.Clear();
	}

	private void ClearLog(string group)
	{
		instance.lineDictionary.Remove(group);
	}

	private Texture2D MakeTex(int width, int height, Color col)
	{
		Color[] array = new Color[width * height];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = col;
		}
		Texture2D texture2D = new Texture2D(width, height);
		texture2D.SetPixels(array);
		texture2D.Apply();
		return texture2D;
	}

	private void OnDestroy()
	{
		Application.logMessageReceived -= HandleLog;
	}

	private void Update()
	{
		if (tmpTime == 0f)
		{
			tmpTime = Time.deltaTime;
		}
		else
		{
			tmpTime = (tmpTime + Time.deltaTime) / 2f;
		}
		if (Time.frameCount % 15 == 0)
		{
			deltaTime = tmpTime;
			tmpTime = 0f;
			if (averageDeltaTime == 0f)
			{
				averageDeltaTime = deltaTime;
			}
			else
			{
				averageDeltaTime = (averageDeltaTime + deltaTime) / 2f;
			}
		}
	}
}
